{% extends 'mobile_app/base.html' %}
{% load static %}

{% block title %}余额查询 - 郑州移不动{% endblock %}

{% block content %}
{% csrf_token %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="mb-0">
                <i class="fas fa-search text-primary me-2"></i>余额查询
            </h2>
            <div>
                <span class="badge bg-primary fs-6">
                    <i class="fas fa-phone me-1"></i>{{ phone.number }}
                </span>
            </div>
        </div>
    </div>
</div>

<!-- 余额概览 -->
<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100 balance-card">
            <div class="card-body text-center text-white">
                <i class="fas fa-wallet mb-3" style="font-size: 2.5rem;"></i>
                <h3 class="fw-bold">¥{{ balance.amount|floatformat:2 }}</h3>
                <p class="mb-0">账户余额</p>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100 data-card">
            <div class="card-body text-center text-white">
                <i class="fas fa-wifi mb-3" style="font-size: 2.5rem;"></i>
                <h3 class="fw-bold">{{ balance.data_remaining }}MB</h3>
                <p class="mb-0">剩余流量</p>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100 voice-card">
            <div class="card-body text-center text-white">
                <i class="fas fa-phone mb-3" style="font-size: 2.5rem;"></i>
                <h3 class="fw-bold">{{ balance.voice_remaining }}分钟</h3>
                <p class="mb-0">剩余语音</p>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100 sms-card">
            <div class="card-body text-center text-white">
                <i class="fas fa-sms mb-3" style="font-size: 2.5rem;"></i>
                <h3 class="fw-bold">{{ balance.sms_remaining }}条</h3>
                <p class="mb-0">剩余短信</p>
            </div>
        </div>
    </div>
</div>

<!-- 套餐信息和使用情况 -->
<div class="row g-4 mb-4">
    <!-- 套餐信息 -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0">
                    <i class="fas fa-mobile-alt me-2"></i>当前套餐
                </h5>
            </div>
            <div class="card-body">
                {% if phone_plan %}
                    <h4 class="text-primary mb-3">{{ phone_plan.plan.name }}</h4>
                    <p class="text-muted mb-4">{{ phone_plan.plan.description }}</p>
                    
                    <div class="row text-center">
                        <div class="col-3">
                            <h6 class="text-success">¥{{ phone_plan.plan.monthly_fee }}</h6>
                            <small class="text-muted">月费</small>
                        </div>
                        <div class="col-3">
                            <h6 class="text-info">{{ phone_plan.plan.data_allowance }}MB</h6>
                            <small class="text-muted">流量</small>
                        </div>
                        <div class="col-3">
                            <h6 class="text-warning">{{ phone_plan.plan.voice_allowance }}分钟</h6>
                            <small class="text-muted">语音</small>
                        </div>
                        <div class="col-3">
                            <h6 class="text-danger">{{ phone_plan.plan.sms_allowance }}条</h6>
                            <small class="text-muted">短信</small>
                        </div>
                    </div>
                    
                    <div class="mt-4">
                        <h6 class="text-muted">使用进度</h6>
                        
                        <!-- 流量使用进度 -->
                        <div class="mb-3">
                            <div class="d-flex justify-content-between mb-1">
                                <small class="text-muted">流量使用</small>
                                <small class="text-muted">
                                    {{ data_used }}MB / {{ phone_plan.plan.data_allowance }}MB
                                </small>
                            </div>
                            <div class="progress" style="height: 8px;">
                                <div class="progress-bar bg-info" role="progressbar" 
                                     style="width: {% widthratio data_used phone_plan.plan.data_allowance 100 %}%">
                                </div>
                            </div>
                        </div>
                        
                        <!-- 语音使用进度 -->
                        <div class="mb-3">
                            <div class="d-flex justify-content-between mb-1">
                                <small class="text-muted">语音使用</small>
                                <small class="text-muted">
                                    {{ voice_used }}分钟 / {{ phone_plan.plan.voice_allowance }}分钟
                                </small>
                            </div>
                            <div class="progress" style="height: 8px;">
                                <div class="progress-bar bg-warning" role="progressbar" 
                                     style="width: {% widthratio voice_used phone_plan.plan.voice_allowance 100 %}%">
                                </div>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="text-center text-muted">
                        <i class="fas fa-exclamation-triangle mb-3" style="font-size: 3rem;"></i>
                        <h5>未选择套餐</h5>
                        <p>请先为您的号码选择一个套餐</p>
                        <a href="{% url 'mobile_app:plan_management' phone.id %}" class="btn btn-primary">
                            <i class="fas fa-cog me-2"></i>选择套餐
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- 快速操作 -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-success text-white">
                <h5 class="mb-0">
                    <i class="fas fa-bolt me-2"></i>快速操作
                </h5>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="{% url 'mobile_app:payment' phone.id %}" class="btn btn-outline-success">
                        <i class="fas fa-credit-card me-2"></i>立即缴费
                    </a>
                    <a href="{% url 'mobile_app:plan_management' phone.id %}" class="btn btn-outline-primary">
                        <i class="fas fa-cog me-2"></i>套餐管理
                    </a>
                    <button class="btn btn-outline-info" onclick="refreshBalance()">
                        <i class="fas fa-sync-alt me-2"></i>刷新余额
                    </button>
                </div>
                
                <!-- 模拟使用功能 -->
                {% if phone_plan %}
                <hr>
                <h6 class="text-muted mb-3">模拟使用</h6>
                <div class="row g-2">
                    <div class="col-4">
                        <button class="btn btn-sm btn-outline-info w-100 simulate-usage" 
                                data-phone-id="{{ phone.id }}" data-usage-type="data" data-amount="10">
                            使用10MB
                        </button>
                    </div>
                    <div class="col-4">
                        <button class="btn btn-sm btn-outline-warning w-100 simulate-usage" 
                                data-phone-id="{{ phone.id }}" data-usage-type="voice" data-amount="5">
                            通话5分钟
                        </button>
                    </div>
                    <div class="col-4">
                        <button class="btn btn-sm btn-outline-danger w-100 simulate-usage" 
                                data-phone-id="{{ phone.id }}" data-usage-type="sms" data-amount="1">
                            发送1条短信
                        </button>
                    </div>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 使用记录 -->
{% if usage_records %}
<div class="row">
    <div class="col-12">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-light">
                <h5 class="mb-0">
                    <i class="fas fa-history me-2"></i>最近使用记录
                </h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>使用时间</th>
                                <th>类型</th>
                                <th>使用量</th>
                                <th>描述</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in usage_records %}
                            <tr>
                                <td>{{ record.usage_time|date:"Y-m-d H:i" }}</td>
                                <td>
                                    <span class="badge bg-{% if record.usage_type == 'data' %}info{% elif record.usage_type == 'voice' %}warning{% else %}danger{% endif %}">
                                        {% if record.usage_type == 'data' %}
                                            <i class="fas fa-wifi me-1"></i>流量
                                        {% elif record.usage_type == 'voice' %}
                                            <i class="fas fa-phone me-1"></i>语音
                                        {% else %}
                                            <i class="fas fa-sms me-1"></i>短信
                                        {% endif %}
                                    </span>
                                </td>
                                <td class="fw-bold">
                                    {{ record.amount_used }}
                                    {% if record.usage_type == 'data' %}MB
                                    {% elif record.usage_type == 'voice' %}分钟
                                    {% else %}条{% endif %}
                                </td>
                                <td>{{ record.description|default:"-" }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endif %}

<!-- 返回按钮 -->
<div class="row mt-4">
    <div class="col-12 text-center">
        <a href="{% url 'mobile_app:phone_management' %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-2"></i>返回号码管理
        </a>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function refreshBalance() {
    location.reload();
}

function simulateUsage(phoneId, usageType, amount) {
    // 防止重复点击
    if (window.simulateUsageInProgress) {
        return;
    }
    
    window.simulateUsageInProgress = true;
    
    $.ajax({
        url: '{% url "mobile_app:simulate_usage" %}',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            phone_id: phoneId,
            usage_type: usageType,
            amount: amount
        }),
        headers: {
            'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val()
        },
        success: function(response) {
            if (response.success) {
                alert('使用成功！');
                // 刷新页面显示最新余额
                location.reload();
            } else {
                alert('使用失败：' + response.message);
                window.simulateUsageInProgress = false;
            }
        },
        error: function(xhr, status, error) {
            alert('请求失败：' + error);
            window.simulateUsageInProgress = false;
        }
    });
}

$(document).ready(function() {
    // 模拟使用功能
    $('.simulate-usage').on('click', function() {
        var $button = $(this);
        
        // 防止重复点击
        if ($button.hasClass('disabled') || window.simulateUsageInProgress) {
            return false;
        }
        
        var phoneId = $button.data('phone-id');
        var usageType = $button.data('usage-type');
        var amount = parseInt($button.data('amount') || 10);
        
        var typeNames = {
            'data': 'MB流量',
            'voice': '分钟语音',
            'sms': '条短信'
        };
        
        if (confirm(`确定要使用 ${amount} ${typeNames[usageType]} 吗？`)) {
            // 禁用按钮防止重复点击
            $button.addClass('disabled').prop('disabled', true);
            $button.html('<i class="fas fa-spinner fa-spin"></i> 处理中...');
            
            simulateUsage(phoneId, usageType, amount);
        }
    });
});
</script>
{% endblock %}
